<?php
    // 指定允许其他域名访问  
    header('Access-Control-Allow-Origin:*');
    // 响应类型  
    header('Access-Control-Allow-Methods:POST');
    // 响应头设置  
    header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
    require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
    require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
    // require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
    require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';
    class Register extends CI_Controller{

        public function __construct(){

            parent::__construct();
            $this->load->model('Register/Check_in');//入住
            $this->load->model('Register/Retreat');//退宿
            $this->load->model('Register/Register_manage');//入住/退宿/调宿 记录查询
            $this->load->model('Register/Tune_in');//调宿
            $this->load->helper('url_helper');
            $this->load->library('session');
            // $this->load->helper('date');
            $this->load->helper(array('form', 'url', 'date'));
        }

        //调用U8人员数据
        // public function erp_connect()
        // {
        //     $RY_name = $_POST['RY_name']; //人员姓名
        //     // $RY_name = ""; //人员姓名

        //     $result_json = $this->Check_in->erp_connect($RY_name);

        //     echo $result_json;

        //     return 0;
        // }
        public function check_in_people_select(){

            $name = $_POST['name'];                     //姓名
            $RY_idcardnum = $_POST['RY_idcardnum'];     //身份证号
            $limit = $_POST['limit'];                   //页数

            // $name = "";
            // $idNumber = "";
            // $limit = "";

            $result_json = $this->Check_in->check_in_people_select(
                $name,
                $idNumber,
                $limit
            );

            echo $result_json;

            return 0;
        }

        //入住方法：申请人节点
        public function check_in_applicant(){

            $name = $_POST['name'];                             //姓名
            $sex = $_POST['sex'];                               //性别
            $idNumber = $_POST['idNumber'];                     //身份证号
            $address = $_POST['address'];                       //户籍地址
            $phone = $_POST['phone'];                           //联系方式
            $emergencyContact = $_POST['emergencyContact'];     //紧急联系人
            $relationShip = $_POST['relationShip'];             //联系人关系
            $emergencyPhone = $_POST['emergencyPhone'];         //紧急联系人联系方式
            $department = $_POST['department'];                 //部门
            $job = $_POST['job'];                               //岗位
            $jobNumber = $_POST['jobNumber'];                   //工号
            $temporary = $_POST['temporary'];                   //临时住宿标示

            // $name = "张五"; 
            // $sex = "1"; 
            // $idNumber = "620102200007193934"; 
            // $address = "浙江省宁波市慈溪市";
            // $phone = "13893691560"; 
            // $emergencyContact = "张三";
            // $relationShip = "哥哥"; 
            // $emergencyPhone = "13893691561";
            // $department = "IT部";
            // $job = "开发工程师";
            // $jobNumber = "";
            // $temporary = "";

            $result_json = $this->Check_in->check_in_applicant(
                $name, 
                $sex, 
                $idNumber,
                $address,
                $phone,
                $emergencyContact,
                $relationShip,
                $emergencyPhone,
                $department,
                $job,
                $jobNumber,
                $temporary
            );

            echo $result_json;

            return 0;
        }

        //入住方法：人资节点
        // public function check_in_hr()
        // {
        //     $requestid = $_POST['requestid']; //请求id
        //     $RY_department = $_POST['RY_department']; //部门
        //     $RY_job = $_POST['RY_job']; //岗位
        //     $username = $_POST['username']; //用户名

        //     $result_json = $this->Check_in->check_in_hr(
        //         $requestid,
        //         $RY_department,
        //         $RY_job,
        //         $username
        //     );

        //     echo $result_json;

        //     return 0;
        // }

        //入住方法:宿管员节点
        public function check_in_admin(){

            $name = $_POST['name'];                     //姓名
            $idNumber = $_POST['idNumber'];             //身份证号
            $liveDate = $_POST['liveDate'];             //入住时间
            $area_number = $_POST['area_number'];       //区域编号
            $floor_number = $_POST['floor_number'];     //宿舍楼编号
            $dorm_floor = $_POST['dorm_floor'];         //宿舍楼层号
            $room_number = $_POST['room_number'];       //宿舍房间号
            $bedNumber = $_POST['bedNumber'];           //床位号
            $remarks = $_POST['remarks'];               //备注
            
            // $name = "张五";
            // $idNumber = "620102200007193934";
            // $liveDate = "2020-10-28";
            // $area_number = "1585";
            // $floor_number = "1";
            // $dorm_floor = "3";
            // $room_number = "302";
            // $bedNumber = "4";
            // $remarks = "测试";
            
            $result_json = $this->Check_in->check_in_admin(
                $name,
                $idNumber,
                $liveDate,
                $area_number,
                $floor_number,
                $dorm_floor,
                $room_number,
                $bedNumber,
                $remarks
            );

            echo $result_json;

            return 0;
        }

        //入住筛选界面方法:取宿舍楼
        public function check_condition(){

            $area_number = $_POST['area_number']; //区域名称

            // $area_number = "1585";

            $result_json = $this->Check_in->check_condition($area_number);

            echo $result_json;

            return 0;
        }

        //入住筛选界面方法:取楼层
        public function check_condition_floor(){

            $area_number = $_POST['area_number']; //区域名称
            $floor_number = $_POST['floor_number']; //宿舍楼名称

            // $area_number = "1585";
            // $floor_number = "1";

            $result_json = $this->Check_in->check_condition_floor(
                $area_number,
                $floor_number
            );

            echo $result_json;

            return 0;
        }

        //入住筛选界面方法:取宿舍
        public function check_condition_number(){
            
            $area_number = $_POST['area_number'];    //区域编号
            $floor_number = $_POST['floor_number'];  //宿舍楼编号
            $dorm_floor = $_POST['dorm_floor'];      //宿舍楼层号
            $department = $_POST['department'];      //部门
            $sex = $_POST['sex'];                    //性别

            // $area_number = "1585";
            // $floor_number = "1";
            // $dorm_floor = "3";
            // $department = "注塑一课";
            // $sex = "1";

            $result_json = $this->Check_in->check_condition_number(
                $area_number,
                $floor_number,
                $dorm_floor,
                $department,
                $sex
            );

            echo $result_json;

            return 0;
        }

        //入住筛选界面方法:取床位与柜子
        public function check_condition_bed_table(){

            $area_number = $_POST['area_number'];       //区域编号
            $floor_number = $_POST['floor_number'];     //宿舍楼编号
            $dorm_floor = $_POST['dorm_floor'];         //宿舍楼层号
            $room_number = $_POST['room_number'];       //宿舍房间号

            // $area_number = "1585";
            // $floor_number = "1";
            // $dorm_floor = "3";
            // $room_number = "302";

            $result_json = $this->Check_in->check_condition_bed_table(
                $area_number,
                $floor_number,
                $dorm_floor,
                $room_number
            );

            echo $result_json;

            return 0;
        }

        
        //入住人员查询方法
        public function check_in_select()
        {
            $date_start = $_POST['date_start'];//开始时间
            $date_end = $_POST['date_end'];//截至时间
            $type = $_POST['type'];//类型
            $RY_name = $_POST['RY_name']; //人员名称
            $RY_degsonid = $_POST['RY_degsonid'];//工号
            $limit = $_POST['limit'];//页数

            // $date_start = "";
            // $date_end = "";
            // $type = "0";
            // $RY_name = "郝军强";
            // $RY_degsonid = "";
            // $limit = "1";
            $result_json = $this->Register_manage->check_in_select(
                $date_start, 
                $date_end, 
                $type, 
                $RY_name, 
                $RY_degsonid, 
                $limit
            );

            echo $result_json;

            return 0;
        }

        //退宿人员信息方法
        public function retreat_select(){

            $name = $_POST['name'];             //姓名
            $jobNumber = $_POST['jobNumber'];   //工号

            // $name = "陈羿华";             //姓名
            // $jobNumber = "IT00053";        //工号

            $result_json = $this->Retreat->retreat_select(
                $name,
                $jobNumber
            );

            echo $result_json;

            return 0;
        }



        //入住/调宿/退宿人员查询方法
        public function dorm_manage_select(){
            
            $date_start = $_POST['date_start'];  //开始日期
            $date_end = $_POST['date_end'];      //结束日期
            $type = $_POST['type'];              //查询类型
            $name = $_POST['name'];              //姓名
            $jobNumber = $_POST['jobNumber'];    //工号
            $limit = $_POST['limit'];            //页数

            // $date_start = "";
            // $date_end = "";
            // $type = "2";
            // $name = ""; 
            // $jobNumber = "";
            // $limit = "1";

            $count = $this->Register_manage->dorm_manage_select_all(
                $date_start, 
                $date_end, 
                $type, 
                $name, 
                $jobNumber, 
                $limit
            );
            $result_json = $this->Register_manage->dorm_manage_select(
                $date_start, 
                $date_end, 
                $type, 
                $name, 
                $jobNumber, 
                $limit,
                $count
            );
            echo $result_json;

            return 0;
        }

        //退宿领用物品查询
        public function use_effects_select(){

            // $jobNumber = $_POST['jobNumber'];  //工号

            $jobNumber = "IT00053";

            $result_json = $this->Retreat->use_effects_select($jobNumber);

            echo $result_json;

            return 0;
        }

            //退宿方法
            public function retreat(){

                $name = $_POST['name'];                         //姓名
                $jobNumber = $_POST['jobNumber'];               //工号
                $check_inAddress = $_POST['check_inAddress'];   //入住位置
                $retreatDate = $_POST['retreatDate'];           //退宿日期
                $retreatType = $_POST['retreatType'];           //退宿类型
                $remarks = $_POST['remarks'];                   //备注

                // $name = "陈家豪";
                // $jobNumber = "R816830";
                // $check_inAddress = "1585*2*B11*2";
                // $retreatDate = "2020-10-29";
                // $retreatType = "1";
                // $remarks = "测试";

                // $result_json = $this->Retreat->retreat($RY_idcardnum, $Retreat_date, $RY_retreat_type, $RY_remarks, $RY_reason, $SQ_return);//$SQ_returneffects
                $result_json = $this->Retreat->retreat(
                    $name,  
                    $jobNumber,
                    $check_inAddress,
                    $retreatDate,
                    $retreatType,
                    $remarks
                );//$SQ_return $RY_reason

                echo $result_json;

                return 0;
            }

            // //调宿人员查询方法
            public function  tune_in_select(){

                $name = $_POST['name'];             //姓名
                $jobNumber = $_POST['jobNumber'];   //工号

                // $name = "张豆豆";
                // $jobNumber = "R015971";

                $result_json = $this->Tune_in->tune_in_select(
                    $name,
                    $jobNumber
                );

                echo $result_json;

                return 0;
            }

            //调换宿舍信息查询方法:取区域
            public function tune_in_area(){

                $result_json = $this->Tune_in->tune_in_area();

                echo $result_json;

                return 0;
            }

            //调换宿舍信息查询方法:取宿舍楼
            public function tune_in_dorm(){

                // $area_number = $_POST['area_number'];//区域编号

                $area_number = "1585";

                $result_json = $this->Tune_in->tune_in_dorm($area_number);

                echo $result_json;

                return 0;
            }

            //调换宿舍信息查询方法:取楼层
            public function tune_in_floor(){

                $area_number = $_POST['area_number'];       //区域编号
                $floor_number = $_POST['floor_number'];     //宿舍楼编号

                // $area_number = "1585";
                // $floor_number = "1";  

                $result_json = $this->Tune_in->tune_in_floor(
                    $area_number,
                    $floor_number
                );
                // $str = '';
                // for($i=1 ;$i<=$result_json->SSL_floor ;$i++){
                //     $str = $str.$i.'*';
                // }
                // $str = substr($str ,0 ,-1);

                // $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $str);
                // $result = json_encode($return_su);
                
                echo $result_json;

                return 0;
            }

            //调换宿舍信息查询方法:取宿舍
            public function tune_in_room(){

                $area_number = $_POST['area_number'];    //区域编号
                $floor_number = $_POST['floor_number'];  //宿舍楼编号
                $dorm_floor = $_POST['dorm_floor'];      //宿舍楼层号

                // $area_number = "1585";      
                // $floor_number = "1";       
                // $dorm_floor =   "3";        

                $result_json = $this->Tune_in->tune_in_room(
                    $area_number,
                    $floor_number,       
                    $dorm_floor   
                );

                echo $result_json;

                return 0;

            }

            //调换宿舍信息查询方法:取床位
            public function tune_in_bed(){

                $area_number = $_POST['area_number'];       //区域编号
                $floor_number = $_POST['floor_number'];     //宿舍楼编号
                $room_number = $_POST['room_number'];       //宿舍房间号

                // $area_number = "333";  
                // $floor_number = "3";
                // $room_number = "101";  

                $result_json = $this->Tune_in->tune_in_bed(
                    $area_number,
                    $floor_number,
                    $room_number
                );

                echo $result_json;

                return 0;
            }

            //调宿方法
            public function tune_in(){
                $Coordinating_date = $_POST['Coordinating_date'];       //调宿日期
                $RY_name = $_POST['RY_name'];                           //人员姓名
                $RY_degsonid = $_POST['RY_degsonid'];                   //工号
                $RY_sex = $_POST['RY_sex'];                             //性别
                $RY_department = $_POST['RY_department'];               //部门
                $RY_check_in_address = $_POST['RY_check_in_address'];   //入住位置
                $AR_number = $_POST['AR_number'];                       //区域编号
                $SSL_number = $_POST['SSL_number'];                     //宿舍楼编号
                $SS_number = $_POST['SS_number'];                       //宿舍房间号
                $RY_bed = $_POST['RY_bed'];                             //床位号
                $RY_remarks = $_POST['RY_remarks'];                     //备注

                // $Coordinating_date = "2020-12-22";          //调宿日期
                // $RY_name = "张豆豆";                        //人员姓名
                // $RY_degsonid = "R015971";                   //工号
                // $RY_sex = "1";                              //性别
                // $RY_department = "自动化装配部";             //部门
                // $RY_check_in_address = "333*3*201*1";       //入住位置
                // $AR_number = "1585";                        //区域编号
                // $SSL_number = "1";                          //宿舍楼编号
                // $SS_number = "525";                         //宿舍房间号
                // $RY_bed = "3";                              //床位号
                // $RY_remarks = "测试";                       //备注

                $result_json = $this->Tune_in->tune_in(
                    $Coordinating_date,
                    $RY_name,
                    $RY_degsonid,
                    $RY_sex,
                    $RY_department,
                    $RY_check_in_address,
                    $AR_number,
                    $SSL_number,
                    $SS_number,
                    $RY_bed,
                    $RY_remarks
                );

                echo $result_json;

                return 0;
            }
        

        //入住/退宿/调宿记录excel导出
        public function excel_Register_manage(){

            // 创建一个处理对象实例   
            $objExcel = new \PHPExcel();  
            // 创建文件格式写入对象实例, uncomment   
            $objProps = $objExcel->getProperties();  
            //设置创建者  TODO:
            $objProps->setCreator('admin');
            //描述  
            // $objProps->setDescription("入住信息汇总");  
            $objActSheet = $objExcel->getActiveSheet();
            //单元格赋值
            $objActSheet->setCellValue('A1','姓名');
            $objActSheet->setCellValue('B1','工号');
            $objActSheet->setCellValue('C1','性别');
            $objActSheet->setCellValue('D1','部门');
            $objActSheet->setCellValue('E1','岗位');         
            $objActSheet->setCellValue('F1','区域名称');
            $objActSheet->setCellValue('G1','宿舍楼名称');
            $objActSheet->setCellValue('H1','宿舍房间号');
            $objActSheet->setCellValue('I1','床位号');
            $objActSheet->setCellValue('J1','是否入住');
            $objActSheet->setCellValue('K1','入住时间');
            $objActSheet->setCellValue('L1','是否退宿');
            $objActSheet->setCellValue('M1','退宿时间');
            $num = 2;
            
            $data = $this->Register_manage->excel_livepeople();
            $data = json_decode($data,true);
            
            for ($i=0; $i < count($data['msg']); $i++) { 
                //性别
                if($data['msg'][$i]['sex'] == 0){
                    $data['msg'][$i]['sex'] = '女';
                } else {
                    $data['msg'][$i]['sex'] = '男';
                }
                
                //是否入住
                if($data['msg'][$i]['liveCheck'] == 'X'){
                    $data['msg'][$i]['liveCheck'] = '已入住';
                } else {
                    $data['msg'][$i]['liveCheck'] = '';
                }

                //是否退宿
                if($data['msg'][$i]['quitCheck'] == 'X'){
                    $data['msg'][$i]['quitCheck'] = '已退宿';
                } else {
                    $data['msg'][$i]['quitCheck'] = '';
                }
            }
            
            for ($i=0; $i < count($data['msg']); $i++) {

                $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['name'] , PHPExcel_Cell_DataType::TYPE_STRING);            //姓名
                $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['jobNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);       //工号
                $objActSheet->setCellValueExplicit('C' . $num,$data['msg'][$i]['sex'] , PHPExcel_Cell_DataType::TYPE_STRING);             //性别
                $objActSheet->setCellValueExplicit('D' . $num,$data['msg'][$i]['department'] , PHPExcel_Cell_DataType::TYPE_STRING);      //部门
                $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['job'] , PHPExcel_Cell_DataType::TYPE_STRING);             //岗位 
                $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['area_name'] , PHPExcel_Cell_DataType::TYPE_STRING);       //区域名称
                $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['floor_name'], PHPExcel_Cell_DataType::TYPE_STRING);       //宿舍楼名称
                $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['room_number'], PHPExcel_Cell_DataType::TYPE_STRING);      //宿舍房间号
                $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['bedNumber'], PHPExcel_Cell_DataType::TYPE_STRING);        //床位号
                $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['liveCheck'], PHPExcel_Cell_DataType::TYPE_STRING);        //是否入住
                $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['liveDate'], PHPExcel_Cell_DataType::TYPE_STRING);         //入住日期
                $objActSheet->setCellValueExplicit('L' . $num,$data['msg'][$i]['quitCheck'], PHPExcel_Cell_DataType::TYPE_STRING);        //是够退宿
                $objActSheet->setCellValueExplicit('M' . $num,$data['msg'][$i]['retreatDate'], PHPExcel_Cell_DataType::TYPE_STRING);      //退宿日期
                //设置行的属性  
                $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
                $num = $num + 1;
            }
            
            //设置列的属性  
            $objActSheet->getColumnDimension("A")->setWidth('20px'); // 设置宽度  
            $objActSheet->getColumnDimension("B")->setWidth('20px'); // 设置宽度  
            $objActSheet->getColumnDimension("C")->setWidth('20px'); // 设置宽度  
            $objActSheet->getColumnDimension("D")->setWidth('20px'); // 设置宽度  
            $objActSheet->getColumnDimension("E")->setWidth('20px'); // 设置宽度  
            $objActSheet->getColumnDimension("F")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("G")->setWidth('20px'); // 设置宽度
            $objActSheet->getColumnDimension("H")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("I")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("J")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("K")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("L")->setWidth('20px'); // 设置宽度 
            $objActSheet->getColumnDimension("M")->setWidth('20px'); // 设置宽度 
            $outputFileName = iconv('UTF-8', 'gb2312', '入住/退宿/调宿记录表.xls');  
            //文件直接输出到浏览器  
            header('Pragma:public');  
            header('Expires:0');  
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
            header('Content-Type:application/force-download');  
            header('Content-Type:application/vnd.ms-excel');  
            header('Content-Type:application/octet-stream');  
            header('Content-Type:application/download');  
            header('Content-Disposition:attachment;filename=' . $outputFileName);  
            header('Content-Transfer-Encoding:binary');  
            PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
                    
            //设置对齐方式      
            $objStyleA3 = $objActSheet->getStyle('A3');  
            $objAlignA3 = $objStyleA3->getAlignment();  
            $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
            $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            
            $objExcel = null;
            }
}
